//
// Name :          OpenGLWnd.h
// Description :   Header file for COpenGLWnd OpenGL superclass.
// Documentation : See OpenGLWnd.cpp
// Author :        Charles B. Owen
//

#pragma once

//! Superclass for MFC windows that use OpenGL

//! This is a class derived from CWnd that will automatically set up
//! for OpenGL.  You can use this whereever you would use
//! CWnd and you'll automatically have OpenGL in this window. 
//! See <a href="https://www.cse.msu.edu/~cse472/secure/installcopenglwnd.html">
//! Installing and Using COpenGLWnd for details.</a>

#include <gl/gl.h>
#include <gl/glu.h>

/////////////////////////////////////////////////////////////////////////////
// COpenGLWnd window

class COpenGLWnd : public CWnd
{
public:
    COpenGLWnd();

    // Operations

    //! Function to draw in OpenGL that a derived class will override.
    /*!
     \param pDC Pointer to a device context active while drawing
    */
    virtual void OnGLDraw(CDC *pDC);

    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(COpenGLWnd)
protected:

    //! Called prior to the window being created. 
    /*! Override this function to make changes to the create structure.
        \param cs Reference to the create structure for the window. */
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    //}}AFX_VIRTUAL

    // Implementation
public:
    //! Support for a menu option to save the current image as a file.
    /*! Call from a menu option. This function brings up the File Save
        dialog box on its own. */
    void OnSaveImage();

    //! Get the size of the window in pixels.
    /*! \param width Where to put the width.
        \param height Where to put the height */
    void GetSize(int &width, int &height);

    //! Get the pixels for the current image.
    /*! Data is in a 2D array in BGR interleaved format. 
        When done with the array, destroy it using: 
        \code 
            delete buffer[0]; 
            delete buffer;
        \endcode
        \param pixels Pointer to location where a pointer will be 
        set that points to the pixel data.  */
    bool ObtainPixels(GLbyte **&pixels);

    virtual ~COpenGLWnd();

    //! Obtain a handle to the OpenGL context.
    HGLRC HGLRc() {return m_hrc;}

    // Generated message map functions
protected:
    //! Set the double buffer status for the window. Default is false.
    /*! This function must be called before the window is created.
        The best place to call this function is in the constructor
        for the window. 
        \param doublebuffer Set to true/false to enable/disable double buffering
    */
    void SetDoubleBuffer(bool doublebuffer);

    //! Get the double buffering status.
    bool GetDoubleBuffer() const {return m_doublebuffer;}

    //! \cond SPECIAL
    //{{AFX_MSG(COpenGLWnd)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnPaint();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    //! \endcond

private:
    int FormattedErrorAfxMsgBox(const char *p_msg);

    bool m_created;
    bool m_doublebuffer;
    BOOL CreateRGBPalette(HDC hDC);

    HGLRC m_hrc;
    CPalette* m_pPal ;		//Palette

   	//
    // Support for generating RGB color palette
    //
    unsigned char ComponentFromIndex(int i, UINT nbits, UINT shift) ;

    static unsigned char   m_oneto8[2];
    static unsigned char   m_twoto8[4];
    static unsigned char   m_threeto8[8];
    static int             m_defaultOverride[13];
    static PALETTEENTRY    m_defaultPalEntry[20];
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

